<?php

namespace backend\models;

use Yii;
use yii\web\IdentityInterface;

/**
 * This is the model class for table "tau_operinfo".
 *
 * @property integer $ID
 * @property string $OperID
 * @property integer $TAu_ID
 * @property string $RoleName
 * @property string $username
 * @property string $Pwd
 * @property string $Address
 * @property string $LinkTel
 * @property string $QQ
 * @property string $Email
 * @property string $Mobile
 * @property integer $SortID
 * @property integer $State
 * @property string $auth_key
 *
 * @property AuthAssignment[] $authAssignments
 * @property AuthItem[] $itemNames
 * @property TbaLoginfo[] $tbaLoginfos
 * @property TmeInstockinfo[] $tmeInstockinfos
 * @property TmeOrderinfo[] $tmeOrderinfos
 * @property TmeOutstockinfo[] $tmeOutstockinfos
 */
class TauOperinfo extends \yii\db\ActiveRecord implements  IdentityInterface
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'tau_operinfo';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
         	[['username', 'Pwd', 'LinkTel', 'Mobile'], 'required'],
            [['TAu_ID', 'SortID', 'State'], 'integer'],
            [['OperID'], 'string', 'max' => 10],
            [['RoleName', 'username', 'Pwd', 'Mobile'], 'string', 'max' => 20],
            [['Address'], 'string', 'max' => 150],
            [['LinkTel', 'QQ', 'Email'], 'string', 'max' => 50],
            [['auth_key'], 'string', 'max' => 40]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'ID' => 'ID',
            'OperID' => 'Oper ID',
            'TAu_ID' => 'Tau  ID',
            'RoleName' => 'Role Name',
            'username' => 'Username',
            'Pwd' => 'Pwd',
            'Address' => 'Address',
            'LinkTel' => 'Link Tel',
            'QQ' => 'Qq',
            'Email' => 'Email',
            'Mobile' => 'Mobile',
            'SortID' => 'Sort ID',
            'State' => 'State',
            'auth_key' => 'Auth Key',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getAuthAssignments()
    {
        return $this->hasMany(AuthAssignment::className(), ['user_id' => 'ID']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getItemNames()
    {
        return $this->hasMany(AuthItem::className(), ['name' => 'item_name'])->viaTable('auth_assignment', ['user_id' => 'ID']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getTbaLoginfos()
    {
        return $this->hasMany(TbaLoginfo::className(), ['TAu_ID' => 'ID', 'OperID' => 'OperID']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getTmeInstockinfos()
    {
        return $this->hasMany(TmeInstockinfo::className(), ['TAu_ID' => 'ID', 'OperID' => 'OperID']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getTmeOrderinfos()
    {
        return $this->hasMany(TmeOrderinfo::className(), ['TAu_ID' => 'ID', 'OperID' => 'OperID']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getTmeOutstockinfos()
    {
        return $this->hasMany(TmeOutstockinfo::className(), ['TAu_ID' => 'ID', 'OperID' => 'OperID']);
    }
    //前置过滤器
    public function beforeSave($insert){
    	if(parent::beforeSave($insert)){
    		if($this->isNewRecord){
    			$this->OperID=time();
    
    		}
    		return true;
    	}
    	return false;
    }
    /**
     * 根据给到的ID查询身份。
     *
     * @param string|integer $id 被查询的ID
     * @return IdentityInterface|null 通过ID匹配到的身份对象
     */
    public static function findIdentity($id)
    {
    	return static::findOne(['ID'=>$id]);
    }
     
    /**
     * 根据 token 查询身份。
     *
     * @param string $token 被查询的 token
     * @return IdentityInterface|null 通过 token 得到的身份对象
     */
    public static function findIdentityByAccessToken($token, $type = null)
    {
    	return static::findOne(['access_token' => $token]);
    }
     
    /**
     * @return int|string 当前用户ID
     */
    public function getId()
    {
    	return $this->ID;
    }
     
    /**
     * @return string 当前用户的（cookie）认证密钥
     */
    public function getAuthKey()
    {
    	return $this->auth_key;
    }
     
    /**
     * @param string $authKey
     * @return boolean if auth key is valid for current user
     */
    public function validateAuthKey($authKey)
    {
    	return $this->getAuthKey() === $authKey;
    }
}
